package cn.shanke.datax.transformer;

import com.alibaba.datax.common.element.Record;
import com.alibaba.datax.common.element.StringColumn;
import com.alibaba.datax.common.exception.DataXException;
import com.alibaba.datax.transformer.ComplexTransformer;

import java.util.Arrays;
import java.util.Map;

public class AllTransformer extends ComplexTransformer {

   public AllTransformer() {
       super.setTransformerName("alltransformer");
   }

    /**
     * @param record   行记录，UDF进行record的处理后，更新相应的record
     * @param map transformer运行的配置项
     * @param objects    transformer函数参数
     */
    @Override
    public Record evaluate(Record record, Map<String, Object> map, Object... objects) {
//        System.out.println(this.getTransformerName() + " - map : " + map);
//        System.out.println(this.getTransformerName() + " - objects : " + Arrays.toString(objects));

        int columnIndex;
        String replaceString;
        try {
            if (objects.length != 2) {
                throw new RuntimeException("alltransformer paras must be 2");
            }

            columnIndex = (Integer) objects[0];
            replaceString = (String) objects[1];
        } catch (Exception e) {
            throw DataXException.asDataXException(AllTransformerErrorCode.TRANSFORMER_ILLEGAL_PARAMETER, "paras:" + Arrays.asList(objects).toString() + " => " + e.getMessage());
        }

        record.setColumn(columnIndex, new StringColumn(replaceString));
        return record;
    }
}
